<template>
    <div>
    <el-table
      style="max-height: 300px;overflow-y: auto;width: 100%"
      row-key="id"
      v-loading="loading"
      ref="unitedHouseList"
      default-expand-all
      :data="unitedHouseList"
      tooltip-effect="dark"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      @select="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="房间" width="200">
        <template slot-scope="scope">
          {{ scope.row.roomSign }}{{ scope.row.roomNum }}
          <span class="booking_circle_span orange_color verticalAlignUnset" v-if="scope.row.singleMain == 1 && scope.row.children.length > 0" style="margin-left: 4px;">主</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="150">
        <template slot-scope="scope">
          {{scope.row.peopleName}}
        </template>
      </el-table-column>
      <el-table-column label="房型" width="150">
        <template slot-scope="scope">
          {{scope.row.roomName}}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.isSingle == 0">已结退房</span>
          <span v-if="scope.row.isSingle == 1">正常入住</span>
          <span v-if="scope.row.isSingle == 2">未结退房</span>
          <span v-if="scope.row.isSingle == 3">联房退房</span>
        </template>
      </el-table-column>
      <el-table-column label="入离日期">
        <template slot-scope="scope">
          {{ scope.row.arrivalsTime }}  -  {{ scope.row.predissociationTime }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 15px;text-align: right;"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <span class="dialog-footer" style="float: right;margin-top: 18px;">
      <el-button type="primary" @click="joinRoomSub()" :disabled="this.unitedHouseSelection.length == 0">确定</el-button>
    </span>
  </div>
</template>

<script>
import moment from 'moment';
export default {
  data() {
    return {
      pageIndex: 1,
      loading: false,
      pageSize: 10,
      totalPage: 0,
      unitedHouseList: [],
      unitedHouseSelection: [],
      unitedHouseSelectionId: [],
      item: {},
      dataForm: {
        singleId: ''
      },
      dataRule: {
        sellerId: [{ required: true, message: '商家不能为空', trigger: 'blur' }]
      }
    };
  },
  methods: {
    init(singleId) {
      Object.assign(this.$data, this.$options.data());
      this.dataForm.singleId = singleId;
      this.getDataList();
    },
    //选择联房房间
    async handleSelectionChange(select, row) {
      this.rows = this.$refs.unitedHouseList.selection; //获取表格中所有选中的数据
      let checkrow = [];
      checkrow.push(row);
      if (row.isCheck == true) {
        if (select == 1) {
          this.$refs.unitedHouseList.toggleRowSelection(row, false);
        }
        this.clearRow(checkrow);
      } else {
        if (select == 1) {
          this.$refs.unitedHouseList.toggleRowSelection(row, true);
        }
        this.checkRow(checkrow);
      }

      this.unitedHouseSelection = select;
      this.unitedHouseSelectionId = [];
      select.forEach(item => {
        this.unitedHouseSelectionId.push(item.id);
      });
    },
    //取消选中递归
    clearRow(data) {
      Array.from(data).forEach(row => {
        row.isCheck = false; //给这行数据设置一个选中字段为false
        this.$refs.unitedHouseList.toggleRowSelection(row, false);
        if (row.children) this.clearRow(row.children); //有子集就递归  没子集了就不循环了
      });
    },
    //选中递归
    checkRow(data) {
      Array.from(data).forEach(row => {
        row.isCheck = true; //选中是字段值为true
        this.$refs.unitedHouseList.toggleRowSelection(row, true);
        if (row.children) this.checkRow(row.children);
      });
    },
    getDataList() {
      this.loading = true;
      this.$http({
        url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinsingle/united_house_list_all`),
        method: 'get',
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          singleId: this.dataForm.singleId
        })
      }).then(({ data }) => {
        this.loading = false;
        if (data && data.code === 0) {
          this.unitedHouseList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    //提交联房
    joinRoomSub() {
      this.$http({
        url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinsingle/join_united_house`),
        method: 'post',
        params: this.$http.adornParams({
          singleId: this.dataForm.singleId,
          roomIds: this.unitedHouseSelectionId.join(',')
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.visible = false
          this.$nextTick(() => {
            this.$emit('refreClosing');
          });
          this.$message({
            message: this.$t('Common.OperationSuccessful'),
            type: 'success',
            duration: 1500,
            onClose: () => {

            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    }
  }
};
</script>

<style></style>
